package com.fkframe.codegener.ui.view;

import heip.generator.entity.EntityTable;
import heip.generator.entity.TableClazz;
import heip.generator.generer.ActionGenerator;
import heip.generator.generer.EntityGenerator;
import heip.generator.generer.ServiceDaoGenerator;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.org.rapid_framework.generator.GeneratorProperties;

import com.fkframe.codegener.ui.EETSoftFrame;

public class EntityGenerView extends JPanel implements ActionListener {
	private JTextField txtModule;
	private JTextField txtTable;
	
	private JCheckBox chkSequece;
	private JTextField txtSequence;
	private JCheckBox chkUuid;
	
	private JCheckBox chkService;
	private JCheckBox chkServiceEX;
	private JCheckBox chkMvcAction;
	
	private JCheckBox chkPojoVO;
	private JCheckBox chkTempVO;
	
	private JComboBox entityType;
	
	private JButton btnGener;
	
	private EETSoftFrame parentFrame;
	
	/**
	 * Create the panel.
	 */
	public EntityGenerView(EETSoftFrame parent) {
		parentFrame = parent;
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(0, 4, 3, 3));
		
		// 映射表
		txtModule = new JTextField();
		txtModule.setColumns(10);
		txtTable = new JTextField();
		txtTable.setColumns(10);
		panel.add(new JLabel("模块名(ex:cm.tb): "));
		panel.add(txtModule);
		panel.add(new JLabel("表名(必输): "));
		panel.add(txtTable);
		
		//ID 生成策略
		chkSequece = new JCheckBox(" sequence序列",false);
		txtSequence = new JTextField();
		chkUuid = new JCheckBox("uuid",true);
		panel.add(new JLabel("id生成策略: "));
		panel.add(chkUuid);
		panel.add(chkSequece);
		panel.add(new JLabel());
		//panel.add(txtSequence);
		
		//
		//entityType = new JComboBox(new Object[]{"基本","树","审核"});
		entityType = new JComboBox(new Object[]{"基本"});
		
		//chkPojoVO = new JCheckBox("VO",true);
		//chkTempVO = new JCheckBox("生成临时表VO",false);
		//panel.add(chkPojoVO);
		//panel.add(chkTempVO);
		panel.add(new JLabel("*model类型 :"));
		panel.add(entityType);
		panel.add(new JLabel());
		panel.add(new JLabel());
		
		//生成代码
		panel.add(new JLabel("代码选择："));
		JCheckBox chkEntity = new JCheckBox("model",true);
		chkEntity.setEnabled(false);
		
		chkService = new JCheckBox("dao & service ",true);
		chkMvcAction = new JCheckBox("action",true); 
		panel.add(chkEntity);
		panel.add(chkService);
		panel.add(chkMvcAction);
		
		//按钮
		btnGener = new JButton("生成");
		panel.add(btnGener);
		btnGener.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		btnGener.setEnabled(false);
		
		//
		String tableName= txtTable.getText();
		boolean uuidFlg = chkUuid.isSelected();
		boolean  seqFlg = chkSequece.isSelected();
		String modelType= (String) this.entityType.getSelectedItem();
		
		String moduleName=txtModule.getText();
		moduleName = GenerUtil.generModuleName(moduleName, tableName);
		
		//boolean commonVo = this.chkPojoVO.isSelected();
		//boolean temFlag = this.chkTempVO.isSelected();
		/*if(commonVo&&temFlag){
			JOptionPane.showMessageDialog(parentFrame, "VO只能生成一种");
			btnGener.setEnabled(true);
			return;
		}*/
		//System.out.println(moduleName+" "+ tableName+" "+ seqFlg+" "+commonVo+" "+temFlag);
		
		
		//--------------
		/**
		 * 注意：如果VO继承临时表需要指定外键字段
		 * 一张表一张表生成，不用设置父子关系
		 */
		try{
			TableClazz tableClass =new TableClazz(tableName);
			
			//1生成实体
			EntityGenerator eg = new EntityGenerator(moduleName);
			EntityTable table = new EntityTable(tableName);	//参数为 表名：tc_pick_after,子表对象：sub
			if(!uuidFlg&&seqFlg){
				table.setIdGenerType("sequence");
			}else{
				table.setIdGenerType("uuid");
			}
			eg.generatorModel(table,tableClass,modelType);
			
			//2 dao && service
			if(chkService.isSelected()){
				ServiceDaoGenerator seg = new ServiceDaoGenerator(moduleName);
				seg.generatorEmpty(tableClass, modelType);
			}
			//3 action and view
			if(chkMvcAction.isSelected()){
				ActionGenerator aeg = new ActionGenerator(moduleName);
				aeg.generatorEmpty(tableClass,new HashMap());
			}
			
			Runtime.getRuntime().exec("cmd.exe /c start "+ GeneratorProperties.getRequiredProperty("outRoot"));
		}catch(Exception ee){
			ee.getMessage();
			JOptionPane.showMessageDialog(parentFrame, "生成代码报错："+ee.getMessage());
			btnGener.setEnabled(true);
			return;
		}
		//--------------
		
		btnGener.setEnabled(true);
	}
}